package com.pickup.filter;


import com.pickup.api.NeteaseApi;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.springframework.util.StringUtils;

import java.util.Objects;

public class MusicFilter {

    private volatile static MusicFilter musicFilter;


    private String name;

    private String song;

    // false 歌手名字默认不为空
    private boolean BoolName = false;

    // false 歌曲名称默认不为空
    private boolean BoolSong = false;

    public MusicFilter(String name, String song) {
        this.name = name;
        this.song = song;
    }


    private void init(){
        setName();
        setSong();
    }


    public boolean NameAndSong(String singer, String song){
        if (!getName() && !getSong()){
            return this.song.equals(song) && this.name.equals(singer);
        }
        return getName() || getSong();
    }


    public JsonObject getJsonMusic(){
        init();
        if (!StringUtils.isEmpty(this.song)){
            return (JsonObject) new JsonParser().parse(Objects.requireNonNull(NeteaseApi.getInfo(this.song)));
        }
        else {
            return (JsonObject) new JsonParser().parse(Objects.requireNonNull(NeteaseApi.getInfo(this.name)));
        }
    }

    public Boolean getName() {
        return BoolName;
    }

    public void setName() {
        if (StringUtils.isEmpty(this.name)){
            this.BoolName = StringUtils.isEmpty(this.name);
        }
    }

    public Boolean getSong() {
        return BoolSong;
    }

    public void setSong() {
        if (StringUtils.isEmpty(this.song)){
            this.BoolSong = StringUtils.isEmpty(this.song);
        }
    }
}
